package coursework.senders;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Hashtable;

public class SenderTask implements Runnable{

	private Socket client;
	private AudioSample audio = AudioSample.getInstance();
	private Hashtable<Integer, String> messages = new Hashtable<Integer, String>();
	private int delay;
	private int period;
	private int transmissionDelay;
	private boolean transmitting;
	
	public SenderTask(Socket client, int delay, int period) {
		this.client = client;
		this.delay = delay;
		this.period = period;
		transmitting = false;
		init();
	}
	
	private void init(){
		//Connection
		messages.put(101, "101 CONNECT?");
		messages.put(102, "102 CONNECT ACCEPT");
		messages.put(103, "103 CONNECTED");
		messages.put(104, "104 DELAY TEST");
		messages.put(105, "105 DELAY TEST COMPLETE");
		//Start Transmit
		messages.put(201, "201 START TRANSMIT");
		messages.put(202, "202 TRANSMIT ACCEPT DELAY=");
		//Transmissio
		messages.put(301, "301 SAMPLE #");
		messages.put(302, "302 BLEEP #");
		messages.put(303, "303 RETRANSMIT #");
		messages.put(304, "304 RETRANSMIT #");
		//Stop transmit
		messages.put(401, "401 STOP TRANSMIT");
		messages.put(402, "402 STOP TRANSMIT ACCEPT");
		//Disconnect
		messages.put(501, "501 DISCONNECT");
		messages.put(502, "502 DISCONNECT ACCEPT");

		messages.put(601, "601 UNKNOWN REQUEST");
	}
	
	public void run() {
		try{
			BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); 
			DataOutputStream out = new DataOutputStream(client.getOutputStream());
			
			//Greet/Connect part of protocol
			out.writeBytes(messages.get(101)+"\n");
			String clientMessage = in.readLine();
			while(!clientMessage.equals(messages.get(102))){
				out.writeBytes(messages.get(101)+"\n");
				clientMessage = in.readLine();
			}
			out.writeBytes(messages.get(103)+"\n");
			
			//Connection delay test
			int delaySum = 0;
			int testCount = 5;
			for(int i = 0; i < 5; i++){
				long start = System.currentTimeMillis();
				out.writeBytes(messages.get(104)+"\n");
				clientMessage = in.readLine();
				long end = System.currentTimeMillis();
				if(!clientMessage.equals(messages.get(104))){
					testCount--;
					continue;
				}
				delaySum += (int)end-start;
			}
			transmissionDelay = delaySum/(2*testCount);
			out.writeBytes(messages.get(105));
			
			while(true){
				clientMessage = in.readLine();
				if(!clientMessage.equals(messages.get(201))&&!transmitting)continue;
				if(clientMessage.equals(messages.get(201))){
					out.writeBytes(messages.get(202) + delay + ";" + " PERIOD=" + period + ";\n");
					Thread.sleep(transmissionDelay*2);
					audio.addDataOutputStream(out);
					transmitting = true;
				}
				else if (clientMessage.equals(messages.get(303))){
					int i = clientMessage.indexOf("#");
					String number = clientMessage.substring(i+1);
					int n = Integer.parseInt(number);
					out.writeBytes(messages.get(304) + audio.getSampleByNumber(n));
				}
				else if(clientMessage.equals(messages.get(401))){
					out.writeBytes(messages.get(402));
					audio.removeDataOutputStream(out);
					transmitting = false;
				}
				else if(clientMessage.equals(messages.get(501))){
					out.writeBytes(messages.get(502));
					break;
				}
				//get server input and act on it i.e. bleep
			}
			
			transmitting = false;
			out.close();
			in.close();
			client.close();
			
			
			
			
			
			
			
			//sample.addDataOutputStream(out);
			//Greet client
/*			String serverMessage = messages.get(101) + "\n";
			out.writeBytes(serverMessage + "\n");
			String clientMessage = in.readLine();
			//Keep writing greeting until correct response received
			while(!clientMessage.equals(messages.get(102))){
				//System.out.println("Received from client>"+clientMessage);
				out.writeBytes(serverMessage + "\n");
				clientMessage = in.readLine();
			}
			serverMessage = messages.get(103);
			out.writeBytes(serverMessage + "\n");
			//Connection established
			clientMessage = in.readLine();
			while(!clientMessage.equals(messages.get(501))){
				while(!clientMessage.equals(messages.get(201))){
					clientMessage = in.readLine();		
				}
				serverMessage = messages.get(202) + delay;
				out.writeBytes(serverMessage + "\n");
				sample.addDataOutputStream(out);
				Thread.sleep(10);
				clientMessage = in.readLine();
				while(!clientMessage.equals(messages.get(401))){
					if(clientMessage.equals(messages.get(303))){
						int index = clientMessage.indexOf("#");
						int sampleNo = Integer.parseInt(clientMessage.substring(index+1));
						serverMessage = messages.get(304) + sampleNo + sample.getSampleByNumber(sampleNo);
						out.writeBytes(serverMessage + "\n");
					}
					if(Sender.bleeped){
						serverMessage = messages.get(302) + (delay/period);
						out.writeBytes(serverMessage + "\n");
					}
				}
				serverMessage = messages.get(402);
				out.writeBytes(serverMessage + "\n");
				clientMessage = in.readLine();
			}
			serverMessage = messages.get(502);
			out.writeBytes(serverMessage + "\n");
			in.close();
			out.close();
			client.close();*/
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		String s = "303 RETRANSMIT #1";
		int index = s.indexOf("#");
		int sampleNo = Integer.parseInt(s.substring(index+1));
		System.out.println(sampleNo);
	}
}
